const koa=require("koa");
const bodyParser=require("koa-bodyparser");
const cors=require("koa2-cors");
const registerRouter=require("./routes/index");

require("./model/sql");
const app=new koa();

app.use(cors({
  origin: function (ctx) {
      if (ctx.url === '/test') {
          return "*"; // 允许来自所有域名请求
      }
      return 'http://localhost:8080'; // 这样就能只允许 http://localhost:8080 这个域名的请求了
  },
  exposeHeaders: ['WWW-Authenticate', 'Server-Authorization'],
  maxAge: 5,
  credentials: true,
  allowMethods: ['GET', 'POST', 'DELETE'],
  allowHeaders: ['Content-Type', 'Authorization', 'Accept'],
}))


app.use(bodyParser());
// app.use(cors());
// console.log(cors())
app.use(registerRouter());

app.listen(3001, () => {
  console.log('App listening on port 3001!');
});
